<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Catalog_service_model extends CI_Model {
		function updateAllPath($path = ":00000:", $id = 0) { // переиндексация
			$pages = $this->db->select('parent_id, parent_title, parent_path, parent_parent, parent_order')->from('catalog_parents')->where('parent_parent', $id)->get()->result_array();
			// &.order.#:id:
			if (sizeof($pages) > 0) {
				foreach ($pages as $page) {
					$path_new = $path;
					$path_new .= "&.".to5zeros($page['parent_order']).".#";
					$path_new .= ":".to5zeros($page['parent_id']).":";
					$this->db->where('parent_id', $page['parent_id'])->update('catalog_parents', array('parent_path' => $path_new));
					$this->updateAllPath($path_new, $page['parent_id']);
				}
			}
		}
		function getTree() { // выпадающий список для админки
			$pages = $this->db->select('parent_title, parent_path, parent_id, parent_parent')->from('catalog_parents')->order_by('parent_path', 'asc')->get()->result_array();
			return $pages;
		}
		function show_select($select, $id) {
			$tree = $this->getTree();
			$select->addOption('Корень каталога', 0);
			$cant_select = false;
			$this_segment = 1000;
			foreach ($tree as $t) {
				$attr = array();
				$seg = sizeof(explode("&", $t['parent_path'])) - 1;
				if($seg >= 2){
					continue;
				}
				$i = 0;
				$add_text = "";
				while($i < $seg) {
					$add_text .= "&nbsp;&nbsp;&nbsp;";
					$i++;
				}

				if ($id != 0) {
					if ($t['parent_id'] == $id) {
						$attr['disabled'] = "disabled";
						$this_segment = $seg;
					}

					if ($seg > $this_segment) {
						$cant_select = true;
					}
					if ($seg == $this_segment && $t['parent_id'] != $id) {
						$cant_select = false;
						$this_segment = 10000;
					}
					if ($cant_select) {
						$attr['disabled'] = "disabled";
					}
				}

				$option = $select->addOption($add_text."&rarr;&nbsp;".mb_substr($t['parent_title'], 0, 30), $t['parent_id'], $attr);

			}
			return $select;
		}
		function show_to_droppabel() {
			$tree = $this->getTree();
			$cant_select = false;
			$this_segment = 1000;
			foreach ($tree as $t) {
				$attr = array();
				$seg = sizeof(explode("&", $t['parent_path'])) - 2;
				$i = 0;
				$add_text = "";
				while($i < $seg) {
					$add_text .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					$i++;
				}
				$select[$t['parent_id']]['text'] = $add_text.mb_substr($t['parent_title'], 0, 30);
				$select[$t['parent_id']]['can'] = ($t['parent_parent'] != 0?true:false);
			}
			return $select;
		}
		function getCrumps($path, $url) {
			$allpages = $this->db->select('parent_title, parent_id, parent_curl')->from('catalog_parents')->get()->result_array();
			foreach ($allpages as $allpages_one) {
				$book[$allpages_one['parent_id']] = array('title' => $allpages_one['parent_title'], 'id' => $allpages_one['parent_id'], 'curl' => $allpages_one['parent_curl']);
			}
			$cr = explode("&", $path);
			$crumps = "";
			$k = 0;
			foreach ($cr as $cr_one) {
				preg_match_all('#:[0-9]+:#', $cr_one, $num);
				$num = intval(str_replace(":", "", $num[0][0]));
				if ($k==1) $frst = $book[$num]['curl'];
				if ($num != "0")
				$crumps .= " &raquo; <a href='/".$url."/".$book[$num]['id']."/'>".$book[$num]['title']."</a>";
				$k++;
			}
			return $crumps;
		}
		function show_block() {
			return $this->load->view('modules/catalog/block', null, true);
		}
		function opinions() {
			$data['pages'] = $this->db->select()->from('catalog_opinions')->where('opinion_status', '1')->join('catalog_items', 'catalog_items.item_id = catalog_opinions.opinion_item', 'left')->order_by('opinion_date', 'desc')->limit(3)->get()->result_array();
			return $this->load->view('modules/catalog/opinions', $data, true);
		}
		function get_catalog_from($id, $levels = 10, $text=false) {
			if ($levels > 0) {
				$pages = $this->db->select('parent_id, parent_title, parent_path')->from('catalog_parents')->where('parent_status', '1')->where('parent_parent', $id)->order_by('parent_path', 'asc')->get()->result_array();
				$levels = $levels - 1;
				foreach ($pages as $page) {
					$out[$page['parent_id']] = $page;
					$get = $this->get_catalog_from($page['parent_id'], $levels, $text);
					$items = $this->db->select('item_id, item_title')->from('catalog_items')->where('item_status', '1')->where('item_parent', $page['parent_id'])->order_by('item_order', 'asc')->get()->result_array();
					if (sizeof($get) > 0) $out[$page['parent_id']]['childs'] = $get;
					if (sizeof($items) > 0) $out[$page['parent_id']]['items'] = $items;
				}
				return $out;
			}
		}
		function get_item_link($id) {
//			$item = $this->db->select()->from('catalog_items')->where('item_id', $id)->get()->row_array();
//				if (mb_substr_count($item['item_curl'], ".html") > 0 && mb_substr($item['item_curl'], 0, 1) == "/") {
//					$item_link = $item['item_id'];
//				} else {
//					$parent = $this->db->select()->from('catalog_parents')->where('parent_id', $item['item_parent'])->get()->row_array();
//					$allpages = $this->db->select('parent_title, parent_id, parent_curl')->from('catalog_parents')->get()->result_array();
//					foreach ($allpages as $allpages_one) $book[$allpages_one['parent_id']] = array('title' => $allpages_one['parent_title'], 'id' => $allpages_one['parent_id'], 'curl' => $allpages_one['parent_curl']);
//					$cr = explode("&", $parent['parent_path']);
//					foreach ($cr as $cr_one) {
//						preg_match_all('#:[0-9]+:#', $cr_one, $num);
//						$num = intval(str_replace(":", "", $num[0][0]));
//						if ($num != "0")
//						$crumps .= $book[$num]['curl']."/";
//					}
//					$item_link = "/".$crumps.$item['item_curl'].".html";
//				}
				return $id;
		}
		function getCrumpsForAdmin($path, $url) {
			$allpages = $this->db->select('parent_title, parent_id')->from('catalog_parents')->get()->result_array();
			foreach ($allpages as $allpages_one) {
				$book[$allpages_one['parent_id']] = array('title' => $allpages_one['parent_title'], 'id' => $allpages_one['parent_id']);
			}
			$cr = explode("&", $path);
			$crumps = "";
			foreach ($cr as $cr_one) {
				preg_match_all('#:[0-9]+:#', $cr_one, $num);
				$num = intval(str_replace(":", "", $num[0][0]));
				if ($num != "0")
				$crumps .= " &raquo; <a href='/".$url."/".$book[$num]['id']."/'>".$book[$num]['title']."</a>";
			}
			return $crumps;
		}
}
?>